package org.mspring.mlog.service;

import java.util.Date;
import java.util.List;

import org.mspring.mlog.dao.CatalogDao;
import org.mspring.mlog.entity.Catalog;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2014-01-11
 */
@Service
@Transactional
public class CatalogService {

    @Autowired
    private CatalogDao catalogDao;

    public Catalog findByName(String name) {
        return catalogDao.findByName(name);
    }

    public List<Catalog> listAll() {
        return catalogDao.listAll();
    }

    public Catalog get(Long id) {
        return catalogDao.get(id);
    }

    public Page<Catalog> listPage(QueryBuilder queryBuilder, Page<Catalog> catalogPage) {
        return catalogDao.listPage(queryBuilder, catalogPage);
    }

    public void setCatalogSort(Long[] ids, Long[] orders) {
        if (ids == null || orders == null || ids.length == 0 || orders.length == 0) {
            return;
        }
        if (ids.length != orders.length) {
            return;
        }
        for (int i = 0; i < ids.length; i++) {
            Long id = ids[i];
            Long order = orders[i];
            if (id == null) {
                continue;
            }
            catalogDao.setSort(id, order);
        }
    }

    public long insert(Catalog catalog) {
        if (catalog.getCreateTime() == null) {
            catalog.setCreateTime(new Date());
        }
        return catalogDao.insert(catalog);
    }

    public void update(Catalog catalog) {
        catalogDao.update(catalog);
    }

    public void delete(Long... id) {
        catalogDao.delete(id);
    }

    public void delete(Catalog catalog) {
        catalogDao.delete(catalog);
    }
}
